﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_TIENICH
{
    public partial class frm_timkiemnhanvien : Form
    {
        int currenrowindex;
        public string manhanvien
        {
            get { return txtmanv.Text; }
            set { txtmanv.Text = value; }
        }
        tienichDataContext context = cls_tienich.getcontext();
        public frm_timkiemnhanvien()
        {
            InitializeComponent();
        }

        private void frm_timkiemnhanvien_Load(object sender, EventArgs e)
        {
            try
            {
               

                IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.loaiuser.Value>0 select c; 
                if (nhanviens.Count() > 0)
                {
                    grdata.Rows.Clear();
                    for (int i = 0; i < nhanviens.Count(); i++)
                    {
                        DM_nhanvien nhanvien = nhanviens.ElementAt(i);
                        grdata.Rows.Add();
                        grdata.Rows[i].Cells["manv"].Value = nhanvien.manv;
                        grdata.Rows[i].Cells["tennv"].Value = nhanvien.hoten;


                    }
                    currenrowindex = 0;
                    this.grdata.Rows[currenrowindex].Selected = true;
                    this.txtmanv.Text = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
                }
            }
            catch (Exception ex)
            {
 
            }

        }

        private void grdata_Click(object sender, EventArgs e)
        {
           
        }

        private void grdata_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                currenrowindex = e.RowIndex;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmanv.Text = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
            }
            catch (Exception ex)
            {
            }
        }

        private void grdata_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    currenrowindex = e.RowIndex;
            //    this.grdata.Rows[currenrowindex].Selected = true;
            //    this.txtmanv.Text = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
            //}
            //catch (Exception ex)
            //{
            //}
        }

        private void grdata_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    currenrowindex = e.RowIndex;
            //    this.grdata.Rows[currenrowindex].Selected = true;
            //    this.lbmanv.Text = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
            //}
            //catch (Exception ex)
            //{
            //}

        }

        private void grdata_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                this.Close();

            }
            if (e.KeyCode == Keys.Down)
            {
                try
                {
                    if (currenrowindex < grdata.Rows.Count)
                    {
                        currenrowindex = currenrowindex + 1;
                        this.grdata.Rows[currenrowindex].Selected = true;
                        this.txtmanv.Text = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (e.KeyCode == Keys.Up)
            {
                try
                {
                    if (currenrowindex > 0)
                    {
                        currenrowindex = currenrowindex - 1;
                        this.grdata.Rows[currenrowindex].Selected = true;
                        this.txtmanv.Text = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        private void grdata_SelectionChanged(object sender, EventArgs e)
        {
            
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtkeyword_TextChanged(object sender, EventArgs e)
        {
            string keyword=txtkeyword.Text;
            IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.loaiuser.Value > 0 where c.hoten.Contains(keyword) select c;
            if (nhanviens.Count() > 0)
            {
                grdata.Rows.Clear();
                for (int i = 0; i < nhanviens.Count(); i++)
                {
                    DM_nhanvien nhanvien = nhanviens.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["manv"].Value = nhanvien.manv;
                    grdata.Rows[i].Cells["tennv"].Value = nhanvien.hoten;


                }
                currenrowindex = 0;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmanv.Text = grdata.Rows[currenrowindex].Cells["manv"].Value.ToString();
            }
        }
    }
}
